package com.crb.smartcard;

import android.text.TextUtils;

import com.crb.smartcard.core.OperationCallBack;
import com.crb.smartcard.core.ThreadPoolManager;
import com.crb.smartcard.core.bean.CApdu;
import com.crb.smartcard.core.bean.CardResult;
import com.crb.smartcard.core.bean.GetSimCardIdResult;
import com.crb.smartcard.core.bean.InitResult;
import com.crb.smartcard.core.bean.OpenChannelResult;
import com.crb.smartcard.core.bean.RApdu;
import com.crb.smartcard.core.oma.ReaderType;
import com.crb.smartcard.core.oma.SmartCard;
import com.crb.smartcard.utils.LogUtil;


import java.util.ArrayList;
import java.util.List;


public class SmartCardService {

    private static final String TAG = "SmartCardService";

    public static void initialize(ReaderType readerType, OperationCallBack<InitResult> callBack) {
        SmartCard.getInstance().setmReaderType(readerType);
        ThreadPoolManager.getInstance().execute(() -> {
            SmartCard.getInstance().bindService(new OperationCallBack<InitResult>() {
                @Override
                public void execute(InitResult s) {
                    callBack.execute(s);
                }
            });
        });
    }

    public static void openChannel(String aid, OperationCallBack<OpenChannelResult> callBack) {
        CardResult cardResult = SmartCard.getInstance().execute(aid, "9000");
        OpenChannelResult openChannelResult = new OpenChannelResult();
        if (cardResult.getStatus() == 0) {
            //执行成功
            if ((cardResult.isMatchSw("9000", cardResult.getSw()))) {
                openChannelResult.setResult(true);
                openChannelResult.setMsg("open channel success");
            } else {
                openChannelResult.setResult(false);
                openChannelResult.setMsg("open channel fail,reason-->sw:" + cardResult.getSw());
            }
        } else if (cardResult.getStatus() == -1) {
            //执行失败
            openChannelResult.setResult(false);
            openChannelResult.setMsg("open channel fail,reason-->:" + cardResult.getMessage());
        }
        callBack.execute(openChannelResult);
    }


    public static void transmitApdu(List<CApdu> cApduList, OperationCallBack<List<RApdu>> callBack) {
        List<RApdu> rApduList = new ArrayList<>();
        for (CApdu cApdu : cApduList) {
            CardResult cardResult = SmartCard.getInstance().execute(cApdu.getCapdu(), cApdu.getExpSw());
            LogUtil.d(TAG + "123456", cardResult.toString());
            if (cardResult.getStatus() == 0) {
                //执行成功
                RApdu rApdu = new RApdu();
                rApdu.setIndex(cApdu.getIndex());
                if (!TextUtils.isEmpty(cardResult.getRapdu())) {
                    rApdu.setRapdu(cardResult.getRapdu());
                }
                rApdu.setSw(cardResult.getSw());
                rApduList.add(rApdu);
                if (!cardResult.isMatchSw(cApdu.getExpSw(), cardResult.getSw())) {
                    callBack.execute(rApduList);
                    return;
                }
            } else if (cardResult.getStatus() == -1) {
                //执行失败
                RApdu rApdu = new RApdu();
                rApdu.setIndex(cApdu.getIndex());
                if (!TextUtils.isEmpty(cardResult.getSw())) {
                    rApdu.setSw(cardResult.getSw());
                } else {
                    rApdu.setSw("");
                }
                rApduList.add(rApdu);
                callBack.execute(rApduList);
                return;
            }
        }
        callBack.execute(rApduList);
    }

    public static void closeChannel() {
        SmartCard.closeChannel();
    }

    public static void getSimCardId(OperationCallBack<GetSimCardIdResult> callBack) {
        String apdu1 = "00A4040010D1560001010001600000000100000000";
        String apdu2 = "80CA004400";
        CardResult cardResult = SmartCard.getInstance().execute(apdu1, "9000");
        GetSimCardIdResult getSimCardIdResult = new GetSimCardIdResult();
        if (cardResult.getStatus() == 0) {
            //执行成功
            CardResult execute = SmartCard.getInstance().execute(apdu2, "9000");
            getSimCardIdResult.setResult(true);
            getSimCardIdResult.setMsg("Successful,Get card number");
            getSimCardIdResult.setCardId(execute.getRapdu().substring(execute.getRapdu().length() - 20, execute.getRapdu().length()));
        } else if (cardResult.getStatus() == -1) {
            //执行失败
            getSimCardIdResult.setResult(false);
            getSimCardIdResult.setMsg(cardResult.getMessage());
        }
        callBack.execute(getSimCardIdResult);
        SmartCard.closeChannel();
    }

    public static void closeService() {
        SmartCard.closeService();
    }
}
